package find.prj.prog;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import find.prj.lib.ParallelFileSearch;
import find.prj.lib.Searcher;
import find.prj.lib.SerialFileSearch;

import javax.swing.JScrollPane;
import javax.swing.JCheckBox;



public class FindProject {

	private JFrame frmFindProject;
	private JTextField txtSearchKeyWord;
	private JTextField txtExcludeKeyWord;
	private JTextArea txtRootPaths;
	private JTextArea txtConsole;
	



	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					FindProject window = new FindProject();
					window.frmFindProject.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public FindProject() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frmFindProject = new JFrame();
		frmFindProject.setTitle("Filter Project");
		frmFindProject.setBounds(100, 100, 636, 669);
		frmFindProject.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frmFindProject.getContentPane().setLayout(null);
		
		txtSearchKeyWord = new JTextField();
		txtSearchKeyWord.setBounds(10, 30, 240, 20);
		txtSearchKeyWord.setText("map");
		frmFindProject.getContentPane().add(txtSearchKeyWord);
		txtSearchKeyWord.setColumns(10);
		
		JLabel lblKeyword = new JLabel("seachKeyWord");
		lblKeyword.setBounds(10, 11, 295, 14);
		frmFindProject.getContentPane().add(lblKeyword);
		
		JLabel lblRootpath = new JLabel("rootPaths");
		lblRootpath.setBounds(10, 61, 240, 14);
		frmFindProject.getContentPane().add(lblRootpath);

		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(260, 11, 349, 609);
		frmFindProject.getContentPane().add(scrollPane);
		
		txtConsole = new JTextArea();
		txtConsole.setFont(new Font("Tahoma", Font.PLAIN, 11));
		scrollPane.setViewportView(txtConsole);
		
		txtExcludeKeyWord = new JTextField();
		txtExcludeKeyWord.setText("install");
		txtExcludeKeyWord.setBounds(10, 353, 240, 20);
		frmFindProject.getContentPane().add(txtExcludeKeyWord);
		txtExcludeKeyWord.setColumns(10);
		
		JLabel lblNewLabel_1 = new JLabel("excludeKeyWord");
		lblNewLabel_1.setBounds(10, 333, 240, 14);
		frmFindProject.getContentPane().add(lblNewLabel_1);
		
		final JCheckBox chckbxParallelSearch = new JCheckBox("Parallel Search");
		chckbxParallelSearch.setSelected(true);
		chckbxParallelSearch.setBounds(10, 544, 240, 23);
		frmFindProject.getContentPane().add(chckbxParallelSearch);

		JButton btnFilterFileNames = new JButton("Filter file names");
		btnFilterFileNames.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				Searcher searcher;
				if (chckbxParallelSearch.isSelected()) {
					searcher = new ParallelFileSearch();
				}
				else
					searcher = new SerialFileSearch();

				String searchKeyWord = txtSearchKeyWord.getText();
				String[] rootPaths = txtRootPaths.getText().trim().replaceAll("\\r", "").split("\\n");
				String excludeKeyWord = txtExcludeKeyWord.getText();
				
				long startTime = System.currentTimeMillis();
				String result = searcher.getSearchResults(searchKeyWord, rootPaths, excludeKeyWord);
				long finishTime = System.currentTimeMillis();
				txtConsole.setText(result + "\nWorking time: " + (finishTime - startTime));
			}
		});
		btnFilterFileNames.setBounds(10, 580, 240, 40);
		frmFindProject.getContentPane().add(btnFilterFileNames);
		
		JScrollPane scrollPane_1 = new JScrollPane();
		scrollPane_1.setBounds(10, 81, 240, 241);
		frmFindProject.getContentPane().add(scrollPane_1);
		
		txtRootPaths = new JTextArea();
		txtRootPaths.setFont(new Font("Tahoma", Font.PLAIN, 11));
		txtRootPaths.setText("c:\\garmin\\\r\nc:\\windows\\\r\nc:\\siemens\\\r\nc:\\program files\\");
		scrollPane_1.setViewportView(txtRootPaths);
		
	}
}
